// pages/constmer/order-info/index.js
// pages/service/order-list/index.js
var api = require("../../../../api.js"),
  QQMapWX = require("../../../../qqmap-wx.js"),
  app = getApp();
// 播放器
const innerAudioContext = wx.createInnerAudioContext()
Page({

  data: {
    orderStatus: 1,
    orderText: '已取单',
    soundRecording: {
      tempPath: "",
      duration: "",
      isPlay: false
    }, // 录音
  },

  onLoad: function (options) {
    /**订单详情 */
    var that = this;
    that.setData({
      icons: wx.getStorageSync('site') + '/addons/sd_liferuning/tp/public/uploads/background',
    })
    console.log('订单id',options)
    app.request({
      url: api.default.getOrderInfo,
      data: {
        orderid: options.orderid,
    
      },
      success: function (res) {
        console.log(res)
        var data = res.data;
        console.log(data)
        if (data.yinpin != '' && data.yinpin != 0){
          innerAudioContext.src = data.yinpin;
          innerAudioContext.autoplay = true;
          that.data.soundRecording.tempPath = data.yinpin;
          that.data.soundRecording.duration = innerAudioContext.duration;
        }
        
        
        
        that.setData({
          data: res.data,
        })
      }
    })
    wx.request({
      url: api.default.getPhone,
      data: {
        bid: wx.getStorageSync('bid')
      },
      method: 'get',
      success: function (res) {
        that.setData({
          phoneNumber: res.data.data.phone
        })
      }
    })
    
  },
  /**
   * 拨打电话
   */
  callPhone: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.phoneNumber,
    })
  },
  toPhone:function(e){

    var that = this;
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })

  },
  /**
   * 取消订单
   */
  cancelOrder: function (e) {
    console.log('订单号', e.currentTarget.dataset.order_no);
    wx.showModal({
      title: '提示',
      content: '您是否取消订单,红包不予退款',
      success:function(res){
        if(res.confirm==1){
          app.request({
            url: api.order.out_price,
            data: {
              order_no: e.currentTarget.dataset.order_no
            },
            method: 'POST',
            success: function (ress) {
              
              if (ress.code == 1) {
                console.log(ress);
                wx.showToast({
                  title: '退款成功',
                  duration: 1000,
                  success: function () {
                    setTimeout(function () {
                      app.request({
                        url: api.user.mess,
                        data: {
                          bid: wx.getStorageSync('bid'),
                          openid: wx.getStorageSync('openid'),
                          order_no: e.currentTarget.dataset.order_no,
                          type: 'cancel'
                        },
                        success: function (ddd) {
                          console.log(ddd);
                        }
                      })
                      wx.redirectTo({
                        url: '../order-list/index',
                      })
                    }, 1000)
                  }
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: '退款失败',
                })
              }
            }
          })
        }
      }
    })
  },
  // 确认收货
  confirmOrder: function(e){
    wx.showModal({
      title: '提示',
      content: '您是否确认收货',
      success: function (res) {
        if (res.confirm == 1) {
          app.request({
            url: api.order.confirmOrder,
            data: {
              order_no: e.currentTarget.dataset.order_no
            },
            method: 'POST',
            success: function (ress) {
              if (ress.code == 1) {
                console.log(ress);
                wx.showToast({
                  title: '成功',
                  icon: 'succes',
                  duration: 1000,
                  mask: true
                })
                setTimeout(function () {
                  wx.navigateBack();
                }, 1000);
              } else {
                wx.showModal({
                  title: '提示',
                  content: '收货失败',
                })
              }
            }
          })
        }
      }
    })
  },
  // 预览图片
  previewPic:function(){
    wx.previewImage({
      current: this.data.data.xphoto, // 当前显示图片的http链接
      urls: [this.data.data.xphoto] // 需要预览的图片http链接列表
    })
  },
  /**
   * 录音播放
   */
  soundRecordingPlay: function () {
    let _this = this;
    let _paused = innerAudioContext.paused;
    let _soundRecording = _this.data.soundRecording;
    if (_paused) {
      innerAudioContext.play();
      _soundRecording.isPlay = true;
      setTimeout(function () {
        let _soundRecording = _this.data.soundRecording;
        _soundRecording.isPlay = false;
        _this.setData({
          soundRecording: _soundRecording
        });
      }, _soundRecording.duration * 1000)
    } else {
      innerAudioContext.stop();
      _soundRecording.isPlay = false;
    }
    _this.setData({
      soundRecording: _soundRecording
    });
  },
  // 预览图片
  previewPic: function (e) {
    let _value = e.currentTarget.dataset.value;
    wx.previewImage({
      current: _value, // 当前显示图片的http链接
      urls: [_value] // 需要预览的图片http链接列表
    })
  },
})